package medium

/*
时间复杂度：O(C) Next的调用次数
空间复杂度：O(C) Next的调用次数

*/

type StockSpanner struct {
	Stack [][2]int
}

func Constructor() StockSpanner {
	return StockSpanner{
		Stack: make([][2]int, 0),
	}
}

func (s *StockSpanner) Next(price int) int {
	ans := 1
	// 将每个大于栈顶元素的元素的ans和price存储起来
	for len(s.Stack) > 0 && price >= s.Stack[len(s.Stack)-1][1] {
		ans += s.Stack[len(s.Stack)-1][0]
		s.Stack = s.Stack[:len(s.Stack)-1]
	}
	s.Stack = append(s.Stack, [2]int{ans, price})
	return ans
}

/**
 * Your StockSpanner object will be instantiated and called as such:
 * obj := Constructor();
 * param_1 := obj.Next(price);
 */
